package com.wu.foodsafetyplayer.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dueeeke.videoplayer.exo.ExoMediaPlayerFactory;
import com.dueeeke.videoplayer.ijk.IjkPlayerFactory;
import com.dueeeke.videoplayer.player.VideoView;
import com.wu.foodsafetyplayer.R;
import com.wu.foodsafetyplayer.app.bean.MonitorVideoBean;

/**
 * GridView适配器
 *
 * @author zihao
 */
public class MonitorVideoAdapter extends BaseAdapter {
    private static final String TAG = MonitorVideoAdapter.class.getSimpleName();
    private LayoutInflater inflater;
    private String[] itemNames = new String[]{"摄像头1", "摄像头2", "摄像头3", "摄像头4"};
    private String[] itemUrls = new String[]{"rtsp://10.9.0.102:554/user=admin&password=&channel=0&stream=0.sdp?", "rtsp://10.9.0.155:554/user=admin&password=&channel=0&stream=1.sdp?", "rtsp://10.9.0.106:554/user=admin&password=abc-123&channel=0&stream=0.sdp?", "rtsp://10.9.0.154:554/user=admin&password=&channel=0&stream=0.sdp?"};
    private Context context;
    private MonitorVideoBean monitorVideoBean;

    public MonitorVideoAdapter(Context context, MonitorVideoBean monitorVideoBean) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.monitorVideoBean = monitorVideoBean;
    }

    @Override
    public int getCount() {
        return monitorVideoBean.data.size() > 0 ? monitorVideoBean.data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return monitorVideoBean.data.get(position) != null ? monitorVideoBean.data.get(position) : "";
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_monitor_video, null);
            holder = new ViewHolder();
            holder.itemName = convertView.findViewById(R.id.item_video_name);
            holder.videoView = convertView.findViewById(R.id.item_video);
            holder.videoView.setFocusable(false);
            holder.videoView.setFocusableInTouchMode(false);
            holder.videoView.setScreenScaleType(1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.itemName.setText(monitorVideoBean.data.get(position).name);

        //holder.videoView.setPlayerFactory(IjkPlayerFactory.create());
        holder.videoView.setPlayerFactory(ExoMediaPlayerFactory.create());
        holder.videoView.release();
        holder.videoView.setUrl(monitorVideoBean.data.get(position).url);
        holder.videoView.start();
        return convertView;
    }

    /**
     * 工具类
     */
    private class ViewHolder {
        TextView itemName;
        VideoView videoView;
    }

}
